<template>
  <header class="bg-white">
    <nav class="mx-auto flex max-w-7xl items-center justify-between p-6 lg:px-8" aria-label="Global">
      <div class="flex lg:flex-1">
        <a class="-m-1.5 p-1.5" @click="routePush('/home/home')">
          <!-- <span class="sr-only">xxx科技</span> -->
          <img class="h-8 w-auto inlogo" :src="infos.mainlogo" alt="" />
        </a>
      </div>
      <div class="flex lg:hidden">
        <button type="button" class="headerbut -m-2.5 inline-flex items-center justify-center p-2.5 text-gray-700"
          @click="mobileMenuOpen = true">
          {{ token ? '控制台' : '登录' }}
          <!-- <Bars3Icon class="h-6 w-6" aria-hidden="true" /> -->
        </button>
      </div>
      <div class="hidden lg:flex lg:gap-x-12">
        <a v-for="item in navigation" :key="item.name" class="text-sm font-semibold leading-6 text-gray-900"
          :class="{ tofind: roupath == item.href }" @click="routePush(item.href)">{{ item.name }}</a>
      </div>
      <div class="hidden lg:flex lg:flex-1 lg:justify-end">
        <a @click="routePush(token ? '/welcome' : '/login')"
          class="headerdlbut text-sm font-semibold leading-6 text-gray-900 indexbtn">{{
            token ? '控制台' : '登录' }} </a>
        <a v-if="!token" @click="routePush('/register')"
          class=" headerdlbut text-sm font-semibold leading-6 text-gray-900 indexbtn">{{
            '注册' }} </a>
      </div>
    </nav>
    <Dialog as="div" class="lg:hidden" :open="mobileMenuOpen" @close="mobileMenuOpen = false">
      <div class="fixed inset-0 z-10" />
      <DialogPanel
        class=" fixed inset-y-0 right-0 z-10 w-full overflow-y-auto bg-white px-6 py-6 sm:max-w-sm sm:ring-1 sm:ring-gray-900/10">
        <div class="flex items-center justify-between">
          <a href="#" class="-m-1.5 p-1.5">
            <span class="sr-only">xxx科技</span>
            <img class="inlogo h-8 w-auto" :src="infos.mainlogo" alt="" />
          </a>
          <button type="button" class="h5header_2 -m-2.5 p-2.5 text-gray-700" @click="mobileMenuOpen = false">
            返回
            <XMarkIcon class="h-6 w-6" aria-hidden="true" />
          </button>
        </div>
        <div class="hrclass">
          <hr />
        </div>
        <div class="mt-6 flow-root">
          <div class="-my-6 divide-y divide-gray-500/10">
            <div class="space-y-2 py-6">
              <a v-for="item in navigation" :key="item.name" @click="routePush(item.href)"
                class="navgert -mx-3 block rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">{{
                  item.name }}</a>
            </div>
            <div class="py-6">
              <hr />
              <div class="h5headerdiv">
                <a @click="routePush(token ? '/welcome' : '/login')"
                  class="headerbut2 -mx-3 block rounded-lg px-3 py-2.5 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">{{
                    token ? '控制台' : '登录' }}</a>
                <a @click="routePush(token ? '/home/product' : '/register')"
                  class="headerbut2 -mx-3 block rounded-lg px-3 py-2.5 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50">{{
                    token ? '购物车' : '注册' }}</a>
              </div>
            </div>
          </div>
        </div>
      </DialogPanel>
    </Dialog>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getSettings } from '@/api/setting';
const mobileMenuOpen = ref(false)
import { useRouter, onBeforeRouteUpdate } from "vue-router";
const { currentRoute, replace } = useRouter();
const token = ref(localStorage.getItem('token'))
const infos = ref({});
const navigation = [
  { name: '首页', href: '/home/home' },
  { name: '产品中心', href: '/home/product' },
  { name: '新闻中心', href: '/home/news' },
  { name: '关于我们', href: '/home/about' },
  { name: '联系我们', href: '/home/contact' },
]
onMounted(() => {
  getSettings(['mainlogo']).then((res) => {
    infos.value = res.data.data;
  });
  roupath.value = router.currentRoute.value.fullPath
});
const roupath = ref()
let router = useRouter()
onBeforeRouteUpdate((to) => {
  roupath.value = to.path
});

function routePush(path) {
  console.log(path);
  replace({
    path: path
  });
  mobileMenuOpen.value = false;
}
</script>
<style>
.inlogo {
  width: 200px;
  height: 65px;
}

.indexbtn {
  width: 50px;
  height: 25px;
  margin-left: 5px;
  color: white;
  text-align: center;
  background-color: rgb(14 148 221);
  border-radius: 5px;
}

.tofind {
  color: #0e94ee;
}

.p-6 {
  padding: 0.8rem !important;
}

.p-1\.5 {
  padding: 0 !important;
}

.headerdlbut {
  border-radius: 0px !important;
  margin-left: 20px;
  width: 80px;
  margin-right: 0px;
  color: #007bff;
  text-decoration: none;
  transition: color 0.3s, background-color 0.3s;
  background-color: #f8f9fa;
  padding: 5px 5px;
  border: 1px solid #007bff;
  line-height: 12px;
}

.headerbut {
  width: 80px;
  margin-right: 0px;
  color: #007bff;
  text-decoration: none;
  transition: color 0.3s, background-color 0.3s;
  background-color: #f8f9fa;
  padding: 5px 5px;
  border: 1px solid #007bff;
}

.py-6 {
  padding-top: 0.8rem !important;
  border-top-width: 0px !important;
}

.bg-white {
  --tw-bg-opacity: 0.9 !important;
}

.h5header_2 {
  width: 80px;
  margin-left: 9px;
  color: #007bff;
  text-decoration: none;
  transition: color 0.3s, background-color 0.3s;
  background-color: #f8f9fa;
  padding: 5px 5px;
  border: 1px solid #007bff;
}

.h5headerdiv {
  display: flex;
  justify-content: space-around;
}

.hrclass {
  margin: 10px;
}

.headerbut2 {
  border-radius: 0px !important;
  margin-top: 10px;
  width: 80px;
  color: #007bff;
  text-decoration: none;
  transition: color 0.3s, background-color 0.3s;
  background-color: #f8f9fa;
  padding: 5px 5px;
  border: 1px solid #007bff;
  text-align: center;
}

.navgert {
  color: rgb(107 114 128 / var(--tw-text-opacity));
}
</style>
